<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\FinanceWithdrawOrder;
use App\Http\Model\FinanceKk;

class BlsWithdrawFalseRepair extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'blsWithdrawFalseRepair';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '贝灵斯提现失败数据修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    protected $userTable = 'user';
    protected $frOrderTable = 'finance_recharge_order';
    protected $diamondStreamTable = 'finance_diamond_stream';
    protected $userRegisterLogTable = 'user_register_log';
    protected $userLoginLogTable = 'user_login_log';
    protected $kovStatisticsTable = 'kov_statistics';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orderId = 0;

        do {
            if(empty($orderId)) {
                echo 'orderId空';
                break;
            }
            $model_finance_withdraw_order = new FinanceWithdrawOrder();
            $model_finance_kk = new FinanceKk();

            $orderData = DB::table('finance_withdraw_order')
                ->where('order_id', $orderId)
                ->first();
            $orderData = $this->objToArray($orderData);
            if(empty($orderData)) {
                echo '提现记录查不到';
                break;
            }
            if($orderData['status'] != 1) {
                echo '不是成功的';
                break;
            }

            DB::beginTransaction();

            try{
                $updateOrder = $model_finance_withdraw_order->payOrderStatus($orderData['order_id'], 1, 4, '成功后标记失败');
                if(empty($updateOrder)) {
                    echo '更新失败';
                    break;
                }
                //退钱
                $model_finance_kk->inc($orderData['user_id'], 0, 0, 4, (int)$orderData['kk_amount'], "提现失败-收获金币");

            } catch (\Exception $e) {
                DB::rollBack();
                echo $e->getMessage();
                break;
            }
            DB::commit();

            echo '成功';

        } while (false);

    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

}